package net.huashitong.supervision.report.ctrl;

import net.huashitong.supervision.report.dto.InsideReportDTO;
import net.huashitong.supervision.report.service.ReportService;
import net.huashitong.synthetical.showView.util.DataTimeUtil;
import net.huashitong.util.DateUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/taskreport/")
public class ReportCtrl  {


    @Autowired
    private ReportService service;

    @RequestMapping(value = "/inside/{startDate}/{endDate}",method = RequestMethod.GET)
    public void   getList(HttpServletResponse reponse, @PathVariable String startDate ,@PathVariable String endDate)
    {
      /*  Date sdate = DateUtil.strToDate(startDate);
        Date edate = DateUtil.strToDate(endDate);
        startDate= DateUtil.DateToStr(sdate);
         endDate = DateUtil.DateToStr(edate);*/

        System.out.println(startDate);
        System.out.println(endDate);
//        List<InsideReportDTO> insideReport = service.getInsideReport("", "");

//        HSSFWorkbook export = service.export("1", "2");
        String[]  headers={"创建日期","创建时间",	"提醒日期","轮次",	"任务状态",	"承办人",	"主任务名称",	"子任务名称"};

        List<List<String>> insideReport = service.getInsideReport(startDate, endDate);

        String[] taskHeader={"创建日期",	"创建时间",	"任务期限",	"单位名称",	"任务状态",	"轮次",	"任务名称",	"主任务名称"};
        List<List<String>> taskReport = service.getTaskReport(startDate, endDate);

        String[] loginName={"登陆账号","登陆次数"};
        List<List<String>> loginList = service.getLoginList(startDate, endDate);

        HSSFWorkbook workbook = new HSSFWorkbook();

        workbook = service.exportExcel(workbook, 0, "单位内部", headers, insideReport);
        workbook = service.exportExcel(workbook, 1, "普通任务", taskHeader, taskReport);
        workbook = service.exportExcel(workbook,2,"登陆次数",loginName,loginList);

        OutputStream out = null;
        try {
            out = reponse.getOutputStream();
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
            String nowDate = df.format(new Date());
            String filename = "督查任务报表("+startDate+"~"+endDate+").xls";
            reponse.setContentType("application/x-msdownload");
            reponse.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(filename, "UTF-8"));
            workbook.write(out);
        } catch (Exception e) {

        } finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }






}
